// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let { OPENID, APPID, UNIONID } = wxContext
  let { nickName } = event
  var success = false
  var message = "修改昵称失败"
  var resp = {}
  const db = cloud.database()
  

  var updeteResp = await db.collection('user').where({
    _openid: OPENID
  }).update({
    data: {
      nickName: nickName,
      index: nickName.substring(0, 3)
    }
  }).then(res => {
    console.log("update:", nickName)
    success = true
    message = "修改昵称成功"
    resp = {
      nickName: nickName
    }
  }, error => {
    success = false
    message = "修改昵称失败"
  })
  return {
    success: success,
    message: message,
    data: resp
  }
}